﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XLSupermarket.Cashier.VIP;
using Supermarket;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Sms.V20210111;
using TencentCloud.Sms.V20210111.Models;

namespace XLSupermarket.Cashier.VIP
{
    public partial class Add_VIP : Form
    {
        public Add_VIP()
        {
            InitializeComponent();
        }
        string VerifyCode = "``````";
        /// <summary>
        /// 添加会员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (PhoneRepeatedJudgment()) return;
            string vip_name = Vip_Name.Text;
            string vip_id = "0";
            DateTime Dt = DateTime.Parse(DP_1.Value.ToString("yyyy-MM-dd"));
            string vip_phone = Vip_Phone.Text;
            string other = Other.Text;
            if (VerifyCode != TB_VifeCode.Text)
            {
                MessageBox.Show("验证码输入错误！");
                return;
            }
            string sql = string.Format("insert into VIP_information values('{0}','{1}','{2}','0','{3}')",
                vip_name,Dt,vip_phone,other);
            if (Other.Text.Length<=0)
            {
                sql = string.Format("insert into VIP_information values('{0}','{1}','{2}','0',NULL)",
                vip_name, Dt, vip_phone);
            }
            bool result = DBHelper.Edit(sql) > 0 ? true:false;
            sql = string.Format("select VIP_ID from VIP_information where VIP_TEL='{0}'",vip_phone);
            DataTable dt = DBHelper.FillData(sql);
            if (dt.Rows.Count > 0)
            {
                vip_id = dt.Rows[0][0].ToString();
                if (result) MessageBox.Show(string.Format("添加成功\n您的VIP编号为{0}", vip_id), "提示");
                this.Hide();
            }
            else
            {
                MessageBox.Show("添加失败！");
                Vip_Name.Text = "";
                DP_1.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
                Vip_Phone.Text = "";
                Other.Text = "";
            }
        }
        /// <summary>
        /// 取消添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Vip_Name.Text = "";
            DP_1.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
            Vip_Phone.Text = "";
            Other.Text = "";
        }
        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BT_ViefCode_Click(object sender, EventArgs e)
        {
            try
            {
                string Phone = Vip_Phone.Text;
                Credential cred = new Credential
                {
                    SecretId = "AKIDHOEo79uOcZFemez4sPgiTw84j7E4ey1n",
                    SecretKey = "XooX0C7mAH7gy0lFSQ8ZjcFchV3KL4Xj"
                };
                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("sms.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;
                SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
                SendSmsRequest req = new SendSmsRequest();
                string TEL = "+86";
                TEL += Phone;
                //创建随机数
                Random Rdm = new Random(Guid.NewGuid().GetHashCode());
                //定义一个字符串用来存放6位随机数
                string sRnd = "";
                //for循环产生4个随机数，然后构成一个字符串
                for (int i = 0; i < 6; i++)
                {
                    int iNum = Rdm.Next(0, 10);
                    sRnd += iNum.ToString();
                }
                VerifyCode = sRnd;//验证码
                req.PhoneNumberSet = new string[] { TEL };
                req.SmsSdkAppId = "1400535628";
                req.SignName = "SBQ技术分享";
                req.TemplateId = "1004667";
                req.TemplateParamSet = new string[] { VerifyCode };
                req.SessionContext = "123456";
                SendSmsResponse resp = client.SendSmsSync(req);
                MessageBox.Show("验证码发送成功！\n请注意查看手机短信！", "提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString() + "\n验证码发送失败！");
            }
        }
        /// <summary>
        /// 手机号码重复判断
        /// </summary>
        /// <returns></returns>
        private bool PhoneRepeatedJudgment()
        {
            string sql = "select VIP_TEL from VIP_information";
            DataTable dt = DBHelper.FillData(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][0].ToString() == Vip_Phone.Text)
                {
                    MessageBox.Show("您的号码已存在！");
                    return true;
                }
            }
            return false;
        }
    }
}
